/* for some reason, FireFox can't do scriptaculous effects reliably, so
   I regrettably will be doing browser checking for now, until I figure
   it all out */

var detect = navigator.userAgent.toLowerCase();
var browser;

if (checkIt('safari')) browser = "Safari";
else if (checkIt('opera')) browser = "Opera";
else if (checkIt('msie')) browser = "Internet Explorer";
else if (!checkIt('compatible')) browser = "Netscape Navigator";
else browser = "An unknown browser";

function checkIt(string) {
	var place = detect.indexOf(string) + 1;
	return place;
}



function supportsEffects() {
    if ((browser == "Internet Explorer") || (browser == "Safari")) return true;
    return false;
}

/* ------------ */


function switch_link(name) {
	var link = document.getElementById(name + '_switch');
	if (link.className == 'on') link.className = 'off';
	else link.className = 'on';
}

function request_note_content(note_id) {	
	var url = "/project/get_note_content?note_id=" + note_id;
	url = url + "&tg_format=json";
	url = url + "&stamp=" + new Date().getTime();
	
	function show_note_edit(result) {
		var edit_div  = getElement('edit_note_' + note_id);
		var edit_area = getElement('ta_edit_note_' + note_id);
		var note_disp = getElement('note_content_' + note_id);
		
		edit_area.value = result['note'];
		edit_div.style.width = note_disp.offsetWidth;
		edit_div.style.height = note_disp.offsetHeight;
				
		edit_div.style.display = 'block';
		note_disp.style.display = 'none';
	}
	
	var d = loadJSONDoc(url);
	d.addCallback(show_note_edit);	
}

function cancel_edit(note_id) {
	var edit_div  = getElement('edit_note_' + note_id);
	var note_disp = getElement('note_content_' + note_id);
	
	edit_div.style.display = 'none';
	note_disp.style.display = 'block';
}

function edit_note(note_id) {
    var note_content = getElement('ta_edit_note_' + note_id);
    var url = '/project/edit_note';
    var data = 'note_id=' + note_id + '&note=' + note_content.value;
    
    function edit_note_done(result) {
	var existing_note = getElement('note_' + note_id);
	removeElement(existing_note);
	var notes = getElement('notes');
	notes.innerHTML = result.responseText + notes.innerHTML;
	cancel_edit(note_id);
    }
    
    var req = getXMLHttpRequest();
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8;');
    sendXMLHttpRequest(req, data).addCallback(edit_note_done);
}

function create_new_note() {
    var new_note = getElement('ta_new_note');
    var url = '/project/create_note';
    var data = {'note':new_note.value};
    
    function add_new_note(result) {
        var container = getElement('notes');
        container.innerHTML = result.responseText + container.innerHTML;
        
       
        container.firstChild.style.display = "none";
        new Effect.Appear(container.firstChild);
      
        
        no_notes_msg = getElement('no_notes_message');
        if (no_notes_msg !== null) no_notes_msg.style.display = "none";
        
        toggle('new_note');
    }
    
    var req = getXMLHttpRequest();
    req.open("POST", url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8;');
    sendXMLHttpRequest(req, queryString(data)).addCallback(add_new_note);
}

function remove_note(note_id) {
    var url = '/project/remove_note?note_id=' + note_id;
    
    function note_removed(result) {
        if (result['success'] == true) {
           
        new Effect.Fade('note_' + note_id, { afterFinish: removeElement });
          
        }
    }
    
    loadJSONDoc(url).addCallback(note_removed);
}

function add_risk() {
    var impact = getElement('new_risk_impact').value;
    var chance = getElement('new_risk_chance').value;
    var description = getElement('new_risk_description').value;
    
    var url = '/project/create_risk';
    var data = { 'impact'       : impact,
                 'chance'       : chance,
                 'description'  : description };
    
    function add_new_risk(result) {
        var container = getElement('risks_container');
        container.innerHTML = result.responseText;
        
        new Effect.Highlight(container.firstChild.lastChild);
        
        // TODO: toggle off the no risks message!  (see create_new_note)
        
        toggle('new_risk');
    }
    
    var req = getXMLHttpRequest();
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8;');
    sendXMLHttpRequest(req, queryString(data)).addCallback(add_new_risk);
}

function modify_risk(risk_id) {
    var impact = getElement('risk_impact_' + risk_id).value;
    var chance = getElement('risk_chance_' + risk_id).value;
    
    var data = { 'risk_id'  : risk_id,
                 'impact'   : impact,
                 'chance'   : chance };
    
    var url = '/project/modify_risk?' + queryString(data);
    
    function risk_modified(result) {
        if (result['success'] == true) {
           new Effect.Highlight('risk_' + risk_id);
        }
    }
    
    loadJSONDoc(url).addCallback(risk_modified);
}


function update_project() {    
    var url = '/project/update_project';
    var params = {
        contact_id:  getElement('contact_id').value,
        status:      getElement('project_status').value,
        description: getElement('ta_description').value,
        tg_format:   'json',
        stamp:       new Date().getTime()
    };
    var data = queryString(params);
    
    function update_project_callback(result) {
        result = evalJSONRequest(result);
        if (result['success'] == true) {
            getElement('description_content').innerHTML = result['description'];
            getElement('contact_content').innerHTML = result['contact'];
            new Effect.Highlight('project_name');
        }
    }
    
    var req = getXMLHttpRequest();
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8;');
    sendXMLHttpRequest(req, data).addCallback(update_project_callback);
}


function close_risk(risk_id) {
    var url = '/project/close_risk?risk_id=' + risk_id;
    
    function risk_closed(result) {
        if (result['success'] == true) {
            var risk = getElement('risk_' + risk_id);
            risk.className = "closed";
        }
    }
    
    loadJSONDoc(url).addCallback(risk_closed);
}


function add_issue() {
    var impact = getElement('new_issue_impact').value;
    var description = getElement('new_issue_description').value;
    
    var url = '/project/create_issue';
    var data = { 'impact'       : impact,
                 'description'  : description };
    
    function add_new_issue(result) {
        var container = getElement('issues_container');
        container.innerHTML = result.responseText;
        
        new Effect.Highlight(container.firstChild.lastChild);
        
        // TODO: toggle off the no issues message!  (see create_new_note)
        
        toggle('new_issue');
    }
    
    var req = getXMLHttpRequest();
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8;');
    sendXMLHttpRequest(req, queryString(data)).addCallback(add_new_issue);
}


function modify_issue(issue_id) {
    var impact = getElement('issue_impact_' + issue_id).value;
    
    var data = { 'issue_id' : issue_id,
                 'impact'   : impact };
    
    var url = '/project/modify_issue?' + queryString(data);
    
    function issue_modified(result) {
        if (result['success'] == true) {
           new Effect.Highlight('issue_' + issue_id);
        }
    }
    
    loadJSONDoc(url).addCallback(issue_modified);
}

function close_issue(issue_id) {
    var url = '/project/close_issue?issue_id=' + issue_id;
    
    function issue_closed(result) {
        if (result['success'] == true) {
            var issue = getElement('issue_' + issue_id);
            issue.className = "closed";
        }
    }
    
    loadJSONDoc(url).addCallback(issue_closed);
}


function add_question() {
    var text = getElement('new_question_text').value;
    
    var url = '/project/create_question';
    var data = { 'question' : text };
    
    function add_new_question(result) {
        var container = getElement('questions');
        container.innerHTML = container.innerHTML + result.responseText;
        
       
        hideElement(container.lastChild);
        new Effect.Appear(container.lastChild.id);
      
        
        toggle('new_question');
    }
    
    var req = getXMLHttpRequest();
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8;');
    sendXMLHttpRequest(req, queryString(data)).addCallback(add_new_question);
}


function add_answer(question_id) {
    var text = getElement('new_answer_text_' + question_id).value;
    
    var url = '/project/create_answer';
    var data = { 'question_id'  : question_id,
                 'answer'       : text };
    
    function add_new_answer(result) {
        var container = getElement('answers_' + question_id);
        container.innerHTML = container.innerHTML + result.responseText;
        
        container.lastChild.style.display = "none";
        new Effect.Appear(container.lastChild);
       
        
        toggle('add_answer_' + question_id);
    }
    
    var req = getXMLHttpRequest();
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8;');
    sendXMLHttpRequest(req, queryString(data)).addCallback(add_new_answer);
}


function remove_question(question_id) {
    var url = '/project/remove_question?question_id=' + question_id;
    
    function question_removed(result) {
        if (result['success'] == true) {
          
          	new Effect.Fade('question_' + question_id, { afterFinish: removeElement });
           
        }
    }
    
    loadJSONDoc(url).addCallback(question_removed);
}

function remove_answer(answer_id) {
    var url = '/project/remove_answer?answer_id=' + answer_id;
    
    function answer_removed(result) {
        if (result['success'] == true) {
           
            new Effect.Fade('answer_' + answer_id, { afterFinish: removeElement });
          
        }
    }
    
    loadJSONDoc(url).addCallback(answer_removed);
}
